﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ext.Net;
using WEB.Exceptions;

namespace WEB.Core.Controls
{
    public partial class BasePage
    {
        protected string _MsgTitle = "Notification";

        #region
        protected static string GetMessage(object key)
        {
            return Util.Message.Get(key);
        }
        #endregion

        #region Wait
        [DirectMethod]
        public virtual void ShowWait()
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Message = "Loading, please wait...",
                ProgressText = "Loading...",
                Width = 300,
                Wait = true,
                WaitConfig = new WaitConfig { Interval = 200 }
            });
        }
        [DirectMethod]
        public virtual void HideWait()
        {
            X.Msg.Hide();
        }
        #endregion

        #region Alert
        protected static void AlertMsg(string Title, object msg)
        {
            X.Msg.Alert(Title, msg).Show();
        }
        protected void AlertMsg(object msg)
        {
            AlertMsg(_MsgTitle, msg);
        }
        [DirectMethod]
        public virtual void AlertPerAdd()
        {
            AlertMsg("You do not have permission to add data");
        }
        [DirectMethod]
        public virtual void AlertPerEdit()
        {
            AlertMsg("You do not have permission to edit data");
        }
        [DirectMethod]
        public virtual void AlertPerDelete()
        {
            AlertMsg("You do not have permission to delete data");
        }
        #endregion

        #region Notification
        //public void Notification(Exception ex)
        //{
        //    if (ex is DBException)
        //        Notification((DBException)ex);
        //    else
        //        this.Notification("Notice", ex.Message, string.Empty);
        //}
        //public void Notification(DBException ex)
        //{
        //    this.Notification("Notice", ex.Message, ex.Number);
        //}
        //public void Notification(string message)
        //{
        //    Notification("Notice", message, string.Empty);
        //}
        //public void Notification(string title, string message, int number)
        //{

        //}
        //public void Notification(string title, string message, string detail)
        //{
        //    if (string.IsNullOrEmpty(detail))
        //    {
        //        Ext.Net.Notification.Show(new NotificationConfig
        //        {
        //            Title = title,
        //            ShowPin = true,
        //            Html = message
        //        });
        //    }
        //    else
        //    {
        //        Ext.Net.Notification.Show(new NotificationConfig
        //        {
        //            Title = title,
        //            ShowPin = true,
        //            Tools = new ToolsCollection { new Tool { Type = ToolType.Help, Handler = X.MessageBox.Alert("Chi tiết", detail).ToScript() } },
        //            Html = message
        //        });
        //    }
        //}



        protected static void Notification(string title, string message, int number)
        {
            if (number == 0)
            {
                Ext.Net.Notification.Show(new NotificationConfig
                {
                    Title = title,
                    ShowPin = true,
                    Html = message
                });
            }
        }

        public void Notification(Exception ex)
        {
            if (ex.InnerException is DBException)
            {
                Notification((DBException)ex.InnerException);
            }
            else
            {
                BasePage.Notification(_MsgTitle, ex.Message, 0);
            }
        }
        public void Notification(DBException ex)
        {
            //string message = "";
            if (ex.Number > 0)
            {
                Notification(_MsgTitle, GetMessage(ex.Number), string.Empty);
            }
            else
                if (ex.Number == -2)
                {
                    AlertMsg("Timeout", "Please try again.");
                }
                else
                {
                    AlertMsg("Fail to acces the data. Please try again.");
                }
        }
        protected static void Notification(string message)
        {
            Notification("Notice", message, 0);
        }
        protected static void Notification(string title, string message, string detail)
        {
            if (string.IsNullOrEmpty(detail))
            {
                Ext.Net.Notification.Show(new NotificationConfig
                {
                    Title = title,
                    ShowPin = true,
                    Html = message
                });
            }
            else
            {
                Ext.Net.Notification.Show(new NotificationConfig
                {
                    Title = title,
                    ShowPin = true,
                    Tools = new ToolsCollection
                    {
                        new Tool
                        {
                            Type = ToolType.Help,
                            Handler = X.MessageBox.Alert("Detail", detail).ToScript()
                        } 
                    },
                    Html = message
                });
            }
        }
        #endregion
    }
}
